<template>
<div class="app-container">
  <div style="width: 800px">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input size="small" v-model="form.title" placeholder="请输入标题" />
      </el-form-item>
      <!-- <el-form-item label="分类" prop="classify">
        <el-select size="small" style="width:100%;" v-model="form.classify" placeholder="请选择分类">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="缩略图" required>
        <div style="color:#999">
          建议尺寸650*314px，JPG、PNG格式， 图片小于5M。
        </div>
        <el-upload
          :class="uploadUrl!=''?'hiddenUpload':''"
          action="https://service.bairicun.com/file/upload"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="是否首页显示" prop="top">
        <el-select size="small" style="width:100%;" v-model="form.isShow" placeholder="请选择是否置顶">
          <el-option
            v-for="item in [{
              value: '1',
              label: '是'
            },{
              value: '0',
              label: '否'
            },]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="第三方链接地址" prop="link">
        <div style="color:#ff0000">
          注：如选择第三方链接地址，则不可编辑文章详情
        </div>
        <el-input @input="changeLink" size="small" v-model="form.link" placeholder="请输入第三方链接地址" />
      </el-form-item>
      <el-form-item label="文章描述" prop="description">
        <el-input show-word-limit type="textarea" maxlength="100" size="small" v-model="form.description" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="文章详情" prop="content">
        <tinymce v-model="form.content" :height="300" placeholder="请输入文章详情"/>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit('form')">保存</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import {
  saveApi,
  gettypeListApi
} from '@/api/news'
import Tinymce from '@/components/Tinymce'
export default {
  components: {Tinymce},
  data() {
    return {
      form: {
        title: '',
        link: '',
        isShow: '',
        content: '',
        classify: 24,
        description: ''
      },
      rules: {
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }, ],
        description: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }, ],
        classify: [{
          required: true,
          message: '请选择分类',
          trigger: 'change'
        }, ],
        link: [{
          required: false,
          message: '请输入第三方地址',
          trigger: 'blur'
        }, ],
        isShow: [{
          required: false,
          message: '请选择是否首页显示',
          trigger: 'blur'
        }, ],
        content: [{
          required: true,
          message: '请输入文章详情',
          trigger: 'blur'
        }, ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadStatus: false,
      uploadUrl: '',
      uploadId: '',
      content: '111',
      typeList: []
    }
  },
  mounted() {
    gettypeListApi({
      pageSize: 100,
      pageNum: 1,
    }).then((res) => {
      this.typeList = res.data.result.map((r) => {
        return {
          value: r.id,
          label: r.name
        }
      })
    })
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.uploadUrl = ''
      this.uploadId = ''
    },
    changeLink(e) {
      console.log(e)
      if(e) {
        this.rules.content[0].required = false
      } else {
        this.rules.content[0].required = true
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(e) {
      console.log(e)
      if(e.code == 200) {
        this.uploadStatus = true
        this.uploadUrl = e.data.url
        this.uploadId = e.data.id
      }
    },
    onSubmit(formName) {
      // this.$message('submit!')
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.uploadId) {
            saveApi({
              title: this.form.title,
              typeId: this.form.classify,
              picId: this.uploadId,
              jumpUrl: this.form.link,
              isShow: this.form.isShow==1?1:0,
              content: this.form.content,
              description: this.form.description,
            }).then((res) => {
              this.$message.success('操作成功！');
              setTimeout(() => {
                this.$router.back(-1)
              }, 1000);
            })
          } else {
            this.$message.error('请上传缩略图！');
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel() {
      this.$router.back(-1)
    }
  }
}
</script>
<style>
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>
<style scoped>
.line {
  text-align: center;
}
</style>
